package com.longlong.server;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * 响应信息实体类
 *
 * @author longlong
 */
@Slf4j
public class HttpServletResponse {

    private StringBuilder responseContent = new StringBuilder();

    /**
     * 直接返回 str
     *
     * @param str 返回内容
     */
    public void write(String str){
        this.responseContent.append(str);
    }

    /**
     * 返回文件内容
     *
     * @param path 文件路径
     */
    public void sendRedirect(String path){
        try {
            File file = new File("src/main/java/com/longlong/view/"+path);
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String value = reader.readLine();
            log.info("read file ...");
            while(value!=null){
                System.out.println(value);
                this.responseContent.append(value);
                value = reader.readLine();
            }
            log.info("read file end ...");
        } catch (FileNotFoundException e) {
            log.error("文件不存在");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getResponseContent(){
        return this.responseContent.toString();
    }
}
